<template>
  <div class="app-container">
    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="data"
      element-loading-text="加载数据中"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" type="index" width="60">
      </el-table-column>
      <el-table-column label="头像" align="center" width="80">
        <template slot-scope="scope">
          <el-avatar shape="square" size="small" :src="scope.row.avatar" />
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" width="150">
        <template slot-scope="scope">{{ scope.row.nickname }}</template>
      </el-table-column>
      <el-table-column label="评论文章" align="center" width="150">
        <template slot-scope="scope">{{ scope.row.blog.title }}</template>
      </el-table-column>
      <el-table-column label="评论内容" align="center">
        <template slot-scope="scope">{{ scope.row.content }}</template>
      </el-table-column>
      <el-table-column label="评论日期" align="center" width="230">
        <template slot-scope="scope">{{ formatDate(scope.row.createDate) }}</template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="1500"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="deleteComment(scope.row)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <!--    // 分页-->
      <el-pagination
        style="margin-top: 30px"
        background
        :page-size="site.limit"
        :page-sizes="[5, 10, 20]"
        layout="prev, pager, next, total, ->, sizes, jumper"
        :total="total"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
      />
    </div>
  </div>
</template>

<script>
import { getComment, delComment } from '@/api/comment'
import { formatDate } from '@/utils/tools/formatDate'

export default {
  data() {
    return {
      listLoading: false,
      total: 0, // 总条数
      site: {
        page: 1, // 当前页
        limit: 5 // 显示条数
      },
      totalPage: 0, // 总页数
      data: [], // 数据详情
      pagerCurrentPage: 1 // 分页栏当前页码
    }
  },
  // 一进来就需要获取数据
  created() {
    this.fetchData()
  },
  methods: {
    formatDate,
    // 获取数据
    fetchData() {
      this.listLoading = true
      getComment(this.site).then(({ data }) => {
        this.listLoading = false
        this.data = data.rows.map((item) => {
          return item
        })
        // 处理时间，显示正确的格式
        this.total = data.total // 计算总条数
        this.totalPage = Math.ceil(this.total / this.site.limit) // 总页数
        if (this.site.page > this.totalPage) {
          // 删除文章的时候可能会触发
          this.site.page = this.totalPage
          this.fetchData()
        }
      })
    },
    // 删除留言
    deleteComment({ id }) {
      this.$confirm('是否删除此条评论信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          delComment(id).then((res) => {
            console.log(res)
            this.fetchData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    currentChangeHandle(pageNum) {
      // 页码改变时触发
      this.site.page = pageNum
      this.fetchData()
    },
    // 改变每页显示条数
    sizeChangeHandle(pagerNum) {
      // 显示条数改变
      this.site.limit = pagerNum
      this.fetchData()
    }
  }
}
</script>
<style scoped>
</style>
